package com.tg.demo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tg.demo.common.util.DateUtils;
import com.tg.demo.common.util.UidUtil;
import com.tg.demo.entity.User;
import com.tg.demo.mapper.UserMapper;
import com.tg.demo.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 用户信息表ServiceImpl
 *
 * @author yuanjin
 */
@Service
public class UserServiceImpl implements UserService {
    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Resource
    private UserMapper userMapper;

    @Override
    public List<User> getUserByCondition(User user) {
        List<User> list = userMapper.getUserByCondition(user);
        if (!list.isEmpty()) {
            list.forEach(
                    item -> {
                        item.setCreateTimeStr(DateUtils.dateToStr(item.getCreateTime()));
                        item.setModifyTimeStr(DateUtils.dateToStr(item.getModifyTime()));
                    }
            );
        }
        return list;
    }

    @Override
    public PageInfo<User> getListByPage(User user, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<User> list = this.getUserByCondition(user);
        return PageInfo.of(list);
    }

    @Override
    public User getUserByUserCode(String userCode) {
        List<User> list = userMapper.getUserByCondition(User.builder().userCode(userCode).build());
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    @Override
    public Long insert(User user) {
        Date date = new Date();
        user.setCreateTime(date);
        user.setModifyTime(date);
        return userMapper.insert(user);
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    @Override
    public Long update(User user) {
        user.setModifyTime(new Date());
        return userMapper.update(user);
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    @Override
    public Long delete(String userCode) {
        return userMapper.delete(userCode);
    }

    @Override
    public String getNewUserCode() {
        String userCode = userMapper.getLastUserCode();
        return UidUtil.createUidCode(userCode);
    }

    @Override
    public List<String> getDepartments() {
        return userMapper.getDepartments();
    }

    @Override
    public List<String> getProfessionals(User user) {
        return userMapper.getProfessionals(user);
    }

    @Override
    public List<String> getClassRooms(User user) {
        return userMapper.getClassRooms(user);
    }
}
